package com.csun.dsms.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.csun.dsms.comment.core.utils.wrapper.WrapMapper;
import com.csun.dsms.comment.core.utils.wrapper.Wrapper;
import com.csun.dsms.mapper.DepartmentInfoMapper;
import com.csun.dsms.model.process.DepartmentInfo;
import com.csun.dsms.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentInfoMapper departmentInfoMapper;

    @Override
    public Wrapper<Object> addDepartmentInfo(DepartmentInfo departmentInfo) {
        try {
            departmentInfoMapper.insert(departmentInfo);
            return WrapMapper.ok();
        } catch (Exception e) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, e.getMessage());
        }
    }

    @Override
    public Wrapper<Object> updateDepartmentInfo(DepartmentInfo departmentInfo) {
        try {
            Integer id = departmentInfo.getId();
            if (id == null) {
                return WrapMapper.wrap(Wrapper.ERROR_CODE, "id不能为空");
            }
            departmentInfoMapper.updateByPrimaryKeySelective(departmentInfo);
            return WrapMapper.ok();
        } catch (Exception e) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, e.getMessage());
        }
    }

    @Override
    public Wrapper<Object> deleteDepartmentInfo(DepartmentInfo departmentInfo) {
        try {
            Integer id = departmentInfo.getId();
            if (id == null) {
                return WrapMapper.wrap(Wrapper.ERROR_CODE, "id不能为空");
            }
            departmentInfoMapper.deleteByPrimaryKey(id);
            return WrapMapper.ok();
        } catch (Exception e) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, e.getMessage());
        }
    }

    @Override
    public Wrapper<Object> getDepartmentInfoList(DepartmentInfo departmentInfo) {
        try{
            return WrapMapper.ok(departmentInfoMapper.select(departmentInfo));
        } catch (Exception e) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, e.getMessage());
        }
    }

    @Override
    public Wrapper<Object> getAllDepartment() {
        try {
            List<DepartmentInfo> departmentInfos = departmentInfoMapper.selectAll();
            List<DepartmentInfo> tree = new ArrayList<>();
            Map<String, DepartmentInfo> map = new HashMap<>();
            // 先将所有节点放入 map 中
            for (DepartmentInfo node : departmentInfos) {
                map.put(node.getDepartmentCode(), node);
            }
            // 构建树形结构
            for (DepartmentInfo node : departmentInfos) {
                String parentCode = node.getParentCode();
                if (parentCode == null || !map.containsKey(parentCode)) {
                    // 根节点
                    tree.add(node);
                } else {
                    // 非根节点，添加到父节点的 children 列表中
                    DepartmentInfo parent = map.get(parentCode);
                    if (parent.getChildren() == null) {
                        parent.setChildren(new ArrayList<>());
                    }
                    parent.getChildren().add(node);
                }
            }
            List<JSONObject> resList = new ArrayList<>();
            for (DepartmentInfo node : tree) {
                JSONObject json = convertToJsonByDepartment(node);
                resList.add(json);
            }
            return WrapMapper.ok(resList);
        } catch (Exception e) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, e.getMessage());
        }
    }

    private JSONObject convertToJsonByDepartment(DepartmentInfo node) {
        JSONObject json = new JSONObject();
        json.put("departmentCode", node.getDepartmentCode());
        json.put("departmentName", node.getDepartmentName());
        List<DepartmentInfo> children = node.getChildren();
        if (children != null && !children.isEmpty()) {
            List<JSONObject> childrenJson = new ArrayList<>();
            for (DepartmentInfo child : children) {
                childrenJson.add(convertToJsonByDepartment(child));
            }
            json.put("childType", childrenJson);
        }
        return json;
    }
}
